const express = require("express");
const cors = require("cors");
const fs = require("fs");
const { PrismaClient } = require("@prisma/client");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

const prisma = new PrismaClient(); // 数据库连接实例

app.post("/api/v1/zp", async (req, res) => {
  // const data = JSON.parse(fs.readFileSync('./data/zp-bj.json'));
  // data.push(...req.body);
  // fs.writeFileSync('./data/zp-bj.json', JSON.stringify(data));
  // console.log('存上了,' + data.length);

  const data = req.body.map((item) => {
    return {
      bossAvatar: item.bossAvatar,
      bossName: item.bossName,
      bossTitle: item.bossTitle,
      jobName: item.jobName,
      salaryDesc: item.salaryDesc,
      jobExperience: item.jobExperience,
      jobDegree: item.jobDegree,
      jobLabels: item.jobLabels.join(","),
      welfareList: item.welfareList.join(","),
      skills: item.skills.join(","),
      cityName: item.cityName,
      city: item.city + "",
      areaDistrict: item.areaDistrict,
      businessDistrict: item.businessDistrict,
      brandName: item.brandName,
      brandLogo: item.brandLogo,
      brandStageName: item.brandStageName,
      brandIndustry: item.brandIndustry,
      brandScaleName: item.brandScaleName,
    };
  });
  await prisma.job0905.createMany({
    data,
  });
  const count = await prisma.job0905.count({ where: {} });
  console.log(`当前数据库数据长度:${count}`);
  res.json({
    code: 1,
    data: "成了",
  });
});

app.get("/", (req, res) => {
  res.send("");
});

app.use("/api/v1/dashboard", require("./routes/dashboard"));

app.listen(3339, () => {
  console.log("服务器运行在3339端口");
});
